﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using System.Data;

public partial class Pages_GridViewGioHang : System.Web.UI.UserControl
{

    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    
   BLLSanPham bllSanPham;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void BindData()
    {
        int tongtien = 0;
        int tongsl = 0;
        GridViewGioHang.DataSource = HttpContext.Current.Session["GioHang"];
        GridViewGioHang.DataBind();
        DataTable dt = (DataTable)HttpContext.Current.Session["GioHang"];
        if (dt != null)
        {
            foreach (DataRow dr in dt.Rows)
            {
                tongsl += (int)dr["SoLuong"];
                tongtien += (int)dr["TONGGIA"];
            }
            lbtongsl.Text = tongsl.ToString();
            lbtongtien.Text = tongtien.ToString();
        }
    }

    protected void btmuathem_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }

    protected void btThanhtoan_Click(object sender, EventArgs e)
    {
        if (HttpContext.Current.Session["User"] != null)
        {
            Response.Redirect("HoanTatGioHang.aspx");
        }
        else
            Response.Redirect("Login.aspx");
    }

    protected void btCapNhatGh_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = new DataTable();
            dt = (DataTable)HttpContext.Current.Session["GioHang"];
            if (dt != null)
            {
                foreach (GridViewRow row in GridViewGioHang.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        try
                        {
                            string Masp = GridViewGioHang.DataKeys[row.RowIndex].Value.ToString();

                            int SoLuong = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text);

                            int TongGiaPhaiTra = (int)HttpContext.Current.Session["Gia"] - (int)dt.Rows[row.RowIndex]["TONGGIA"];

                            dt.Rows[row.RowIndex]["SoLuong"] = SoLuong;
                            dt.Rows[row.RowIndex]["TONGGIA"] = SoLuong * (int)dt.Rows[row.RowIndex]["DONGIA"];

                            HttpContext.Current.Session["Gia"] = TongGiaPhaiTra + (int)dt.Rows[row.RowIndex]["TONGGIA"];

                        }
                        catch (FormatException ex)
                        {
                            log.Error(HttpContext.Current.Session["User"] + ":" + ex.Message);
                        }
                    }
                } 
                HttpContext.Current.Session["GioHang"] = dt;
            }
           
        }
        catch { BindData(); }
        BindData();
    }
    protected void GridViewGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DataTable datalist = (DataTable)HttpContext.Current.Session["GioHang"];
        if (e.CommandName == "Remove")
        {
            string item = e.CommandArgument.ToString();
            bllSanPham = new BLLSanPham();

            SanPham dtoSanPham = new SanPham();

            for (int i = 0; i < datalist.Rows.Count; i++)
            {
                if (datalist.Rows[i]["Masp"].ToString().Trim() == item)
                {
                    string items = datalist.Rows[i]["Masp"].ToString().Trim();
                    int SoLuong = (int)datalist.Rows[i]["SoLuong"];
                    dtoSanPham = bllSanPham.iSanPham.selectbyid(items);
                    datalist.Rows.RemoveAt(i);
                    HttpContext.Current.Session["GioHang"] = datalist;
                    HttpContext.Current.Session["Gia"] = (int)HttpContext.Current.Session["Gia"] - dtoSanPham.Dongia * SoLuong;

                    //    datalist.Rows.Remove(dr);
                }
            }
        }
        BindData();
    }
}